package com.peanut.devlibrary.util;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * Created by anonymouslystudio on 2017/6/6.
 */

public class IdentityUtil {
    static String[] ValCodeArr = {"1", "0", "x", "9", "8", "7", "6", "5", "4",
            "3", "2"};
    static String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7",
            "9", "10", "5", "8", "4", "2"};

    public static boolean isValidIdNumber(String idNumber) {
        if (idNumber == null || idNumber.length() != 18) {
            return false;
        }

        try {

            String ai = idNumber.substring(0, 17);
            if (!isNumeric(ai)) {
                return false;
            }

            String yearStr = idNumber.substring(6, 10);
            String monthStr = idNumber.substring(10, 12);
            String dayStr = idNumber.substring(12, 14);
            int year = Integer.valueOf(yearStr);
            int month = Integer.valueOf(monthStr);
            int day = Integer.valueOf(dayStr);

            if (!isValidDate(year, month, day)) {
                return false;
            }

            int totalAiWi = 0;
            for (int i = 0; i < 17; i++) {
                totalAiWi = totalAiWi
                        + Integer.parseInt(String.valueOf(ai.charAt(i)))
                        * Integer.parseInt(Wi[i]);
            }
            int modValue = totalAiWi % 11;
            String strVerifyCode = ValCodeArr[modValue];
            ai = ai + strVerifyCode;

            if (idNumber.length() == 18) {
                if (!ai.equalsIgnoreCase(idNumber)) {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public static String getBirthday(String identity)
    {
        return identity.substring(6, 14);
    }

    public static boolean isValidShortIdNumber(String idNumber) {

        if (idNumber == null || idNumber.length() != 12) {
            return false;
        }
        try {

            String ai = idNumber.substring(0, 11);
            if (!isNumeric(ai)) {
                return false;
            }

            String yearStr = idNumber.substring(0, 4);
            String monthStr = idNumber.substring(4, 6);
            String dayStr = idNumber.substring(6, 8);
            int year = Integer.valueOf(yearStr);
            int month = Integer.valueOf(monthStr);
            int day = Integer.valueOf(dayStr);

            if (!isValidDate(year, month, day)) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private static boolean isValidDate(int year, int month, int day) {

        try {
            GregorianCalendar currentDate = new GregorianCalendar();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);

            if (year < 1000 || month < 0 || day < 0) {
                return false;
            }

            String dateStr = year + "-" + month + "-" + day;
            int curYear = currentDate.get(GregorianCalendar.YEAR);

            long result = currentDate.getTime().getTime() - sdf.parse(dateStr).getTime();
            if ((curYear - year > 150) || result < 0) {
                return false;
            }

            if (month < 1 || month > 12) {
                return false;
            }

            if (day < 0) {
                return false;
            }

            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if (day > 31) {
                        return false;
                    }
                    break;

                case 2:
                    if (currentDate.isLeapYear(curYear)) {
                        if (day > 29) {
                            return false;
                        }
                    } else {
                        if (day > 28) {
                            return false;
                        }
                    }
                    break;

                case 4:
                case 6:
                case 9:
                case 11:
                    if (day > 30) {
                        return false;
                    }
                    break;

                default:
                    return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }


    private static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}
